import * as query from '../services/profile';

const namespace = 'profile';

export default {
  namespace,

  state: {
    data: {},
  },

  effects: {
    * fetch({ payload = {} }, { call, put }) {
      const response = yield call(query.fetch, payload);

      const { errno, data = {} } = response;

      // 解决没有清空的 bug
      data.Banner || Object.assign(data, { Banner: null });

      if (!errno) {
        yield put({
          type: 'save',
          payload: data,
        });
      }
    },
    * update({ payload = {}, callback }, { call, put }) {
      const response = yield call(query.update, payload);
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};